package com.blog.admin.controller;

import com.blog.admin.service.MessageService;
import com.blog.common.core.enums.PathEnum;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.domain.dto.MessageDto;
import com.blog.system.api.feign.RemoteFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 留言后台管理控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/4/16
 **/
@RestController
@Api("留言后台管理")
public class MessageBackController {

    @Resource
    private MessageService messageService;

    @Resource
    private RemoteFileService remoteFileService;

    /**
     * 查询后台评论
     *
     * @param messageDto 查询条件
     * @return {@link AjaxResult}
     */
    @GetMapping("/message/list")
    @ApiOperation("查询后台留言")
    public AjaxResult listMessages(MessageDto messageDto){
        return AjaxResult.success(messageService.listMessages(messageDto));
    }

    /**
     * 审核留言
     *
     * @param id 留言id
     * @param review 是否通过
     * @return {@link AjaxResult}
     */
    @GetMapping("/message/audit")
    @ApiOperation("审核留言")
    public AjaxResult auditMessage(@RequestParam("id") Serializable id, @RequestParam("review") Boolean review){
        messageService.auditMessage(id, review);
        return AjaxResult.success();
    }

    /**
     * 上传留言头像
     *
     * @param multipartFile 文件
     * @return {@link AjaxResult}
     */
    @PostMapping("/message/images")
    @ApiImplicitParam(name = "file", value = "留言头像", required = true, dataType = "MultipartFile")
    @ApiOperation("上传留言头像")
    public AjaxResult saveMessageFile(@RequestPart(value = "file")MultipartFile multipartFile){
        return AjaxResult.success(remoteFileService.upload(PathEnum.MESSAGE, multipartFile).getData());
    }


    /**
     * 新增留言
     *
     * @param messageDto 留言
     * @return {@link AjaxResult}
     */
    @PostMapping("/message")
    @ApiOperation("新增留言")
    public AjaxResult saveMessage (@RequestBody MessageDto messageDto){
        messageService.saveMessage(messageDto);
        return AjaxResult.success();
    }


    /**
     * 新增留言
     *
     * @param id 留言
     * @return {@link AjaxResult}
     */
    @GetMapping("/message")
    @ApiOperation("新增留言")
    public AjaxResult getMessage (@RequestParam("id") Serializable id){
        return AjaxResult.success(messageService.getMessage(id));
    }

    /**
     * 修改留言
     *
     * @param messageDto 留言
     * @return {@link AjaxResult}
     */
    @PutMapping("/message")
    @ApiOperation("修改留言")
    public AjaxResult updateMessage (@RequestBody MessageDto messageDto){
        messageService.updateMessage(messageDto);
        return AjaxResult.success();
    }



}
